package com.laoxu.student.business.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.laoxu.student.business.entity.Class;
import com.laoxu.student.business.entity.Major;
import com.laoxu.student.business.service.ClassService;
import com.laoxu.student.business.vo.ClassVO;
import com.laoxu.student.common.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  班级控制器
 * </p>
 *
 * @author Laoxu
 * @since 2020-04-04
 */
@Api(description="班级管理")
@CrossOrigin //跨域
@RestController
@RequestMapping("/class")
public class ClassController {
    @Autowired
    private ClassService classService;
    
    @ApiOperation("新增或修改班级")
    @PostMapping("save")
    public Result save(@ApiParam(name="class",value = "班级对象",required = true)
                       @RequestBody Class entity){
        classService.saveOrUpdate(entity);
        return Result.ok().data("classId",entity.getId());
    }

    @ApiOperation("根据ID查询班级")
    @GetMapping("{id}")
    public Result get(@ApiParam(name="id",value = "班级ID",required = true)
                      @PathVariable Long id){
        Class entity = classService.getById(id);
        return Result.ok().data("item", entity);
    }

    @ApiOperation("删除多个班级")
    @PostMapping("removes")
    public Result removes(@ApiParam(name="ids",value = "班级ID数组",required = true)
                          @RequestBody List<Long> ids){
        classService.removeByIds(ids);
        return Result.ok();
    }

    @ApiOperation(value = "班级分页列表")
    @GetMapping("{page}/{limit}")
    public Result pageQuery(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit){

        Page<ClassVO> pageParam = new Page<>(page, limit);

        classService.pageQuery(pageParam);
        List<ClassVO> records = pageParam.getRecords();

        long total = pageParam.getTotal();

        return  Result.ok().data("total", total).data("rows", records);
    }

    @ApiOperation(value = "班级列表")
    @GetMapping("list")
    public Result list(@ApiParam(name = "majorId", value = "专业ID", required = false)
                       @RequestParam(required = false) Long majorId){
        QueryWrapper<Class> queryWrapper = new QueryWrapper<>();
        if(majorId!=null){
            queryWrapper.eq("major_id",majorId);
        }
        List<Class> records = classService.list(queryWrapper);

        return  Result.ok().data("items", records);
    }
}

